import java.util.Arrays;

public class LeastInterval {

    public int leastInterval(char[] tasks, int n) {
        int[] nums = new int[26];
        for (int i = 0; i < tasks.length; i++) {
            nums[tasks[i] - 'A']++;
        }
        Arrays.sort(nums);
        int count = 0;
        int temp = nums[nums.length - 1];
        for (int i = nums.length - 1; i >= 0; i--) {
            if (nums[i] == temp) {
                ++count;
            } else break;
        }
        return Math.max(tasks.length, (n + 1) * (nums[25] - 1) + count);
    }
}
